Skip to content

Fix offspring dimensions when crossover is disabled - #388

Draft
linjiaye929 wants to merge 2 commits into
ahmedfgad:masterfrom
linjiaye929:fix-no-crossover-offspring-shape
Draft

linjiaye929 wants to merge 2 commits into
ahmedfgad:masterfrom
linjiaye929:fix-no-crossover-offspring-shape

Conversation

@linjiaye929

Copy link
Copy Markdown

Problem

With crossover disabled, supported retention settings can produce the wrong number of offspring and fail during GA.run().

For a population of eight solutions:

  • num_parents_mating=2, keep_elitism=5 requires three offspring but returns only two rows.
  • num_parents_mating=6, keep_elitism=0, keep_parents=3 requires five offspring but concatenates thirteen rows because the population slice has a negative endpoint.
  • With one selected parent, the first case can silently broadcast that row instead of producing the required offspring array.

Change

Compare the required offspring count with the actual selected-parent count in the non-elitist branch. In the elitist short-offspring branch, retain the existing selected-parent prefix and fill only missing rows from the current population. Existing successful-path ordering is preserved. Add public-lifecycle regressions covering both cases, one-parent and zero-offspring boundaries, and mutation disabled or enabled with zero probability.

Validation

Using native source at da4b0bf6ceaec0cadf9b354c37f34e5e317cbe2a, CPython 3.12.14, NumPy 2.5.4, cloudpickle 3.1.2 and pytest 9.2.0:

  • New tests on the original code: 6 failed, 8 passed.
  • Three existing test files on the original code: 88 passed.
  • Patched new and existing tests: 102 passed, 0 skipped, with two existing warnings. Existing files: test_keep_parents_elitism.py, test_crossover_mutation.py, and test_operator_regressions.py.
  • A separate public-run matrix passed 696 legal retention, parent-selection and mutation combinations. Independent review also checked 16 offspring-content/dtype cases.

The full test suite, wheel build, optional deep-learning tests, other Python/NumPy versions, and hosted CI have not been verified. This PR is a draft.

AI assistance was used for investigation, implementation and test preparation.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

📊 Change summary

3 files changed · +69 / −3 lines

pie showData
    title Lines changed per file (72 total)
    "tests/test_no_crossover_offspring.py" : 63
    "pygad/utils/engine.py" : 7
    "pygad/utils/__init__.py" : 2
Loading
Per-file breakdown
File + − total
tests/test_no_crossover_offspring.py 63 0 63
pygad/utils/engine.py 5 2 7
pygad/utils/__init__.py 1 1 2
Total (3 files) 69 3 72

The unchanged release-comparison test requires a submodule version bump when its source differs from the latest PyPI release. Keep the original fix and tests unchanged.

Verified corrected wheel: 102 relevant regressions, 10 release-version checks, and 8 release-tool checks pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant